<?php

class Code
{
	public static function find($arr = array())
	{
		global $db;

		if (empty($arr))
		{
			$st = $db->prepare("SELECT * FROM project");
		} else if ($arr['pid'] && $arr['type'])
		{
			$st = $db->prepare("SELECT * FROM code WHERE `pid`=:pid AND `type`=:type");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$st->execute($arr);
		$a = $st->fetchAll(PDO::FETCH_CLASS, "Code");
		return $a;
	}
	
	public static function add($arr)
	{
		global $db;
		if ($arr['pid'] && $arr['codetext'] && $arr['type'])
		{
			$st = $db->prepare("INSERT INTO code (`pid`, `type`, `codetext`) VALUES (:pid, :type, :codetext);");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$a = $st->execute($arr);
		return $a; //返回bool
	}	
	
	public static function update($arr)
	{
		global $db;
		if ($arr['pid'] && $arr['type'] && $arr['codetext'])
		{
			$sql = "UPDATE code SET `codetext`='" . $arr['codetext'] . "' WHERE `pid`='" . $arr['pid'] . "' AND `type`='".$arr['type']."'";
			$st = $db->exec($sql);
		}else
		{
			throw new Exception("Unsupported property!");
		}
//		$st->execu($arr);
		if ($st)
			return TRUE;
		
	}
}
?>